package SingleList;

public class SingleLinkedList {
    // 节点内部类
    private static class ListNode {
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
            this.next = null;
        }
    }

    private ListNode head;
    // 链表长度
    private int size;

    // 构造方法
    public SingleLinkedList() {
        head = null;
        size = 0;
    }

    // 头插法
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        if (head == null) {
            head = newNode;
        } else {
            newNode.next = head;
            head = newNode;
        }
        size++;
    }

    // 尾插法
    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        if (head == null) {
            head = newNode;
        } else {
            ListNode cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = newNode;
        }
        size++;
    }

    public boolean addIndex(int index, int data) {
        if (index < 0 || index > size) {
            System.out.println("索引不合法");
            return false;
        }

        if (index == 0) {
            addFirst(data);
            return true;
        }

        if (index == size) {
            addLast(data);
            return true;
        }

        ListNode prev = head;
        for (int i = 0; i < index - 1; i++) {
            prev = prev.next;
        }

        ListNode newNode = new ListNode(data);
        newNode.next = prev.next;
        prev.next = newNode;
        size++;
        return true;
    }

    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    // 删除第一次出现关键字为key的节点
    public void remove(int key) {

        if (head == null) {
            return;
        }

        if (head.val == key) {
            head = head.next;
            size--;
            return;
        }


        ListNode prev = head;
        while (prev.next != null && prev.next.val != key) {
            prev = prev.next;
        }

        if (prev.next != null) {
            prev.next = prev.next.next;
            size--;
        }
    }

    // 删除所有值为key的节点
    public void removeAllKey(int key) {

        if (head == null) {
            return;
        }

        ListNode prev = head;
        ListNode cur = head.next;

        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
                size--;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }


        if (head.val == key) {
            head = head.next;
            size--;
        }
    }

    // 得到单链表的长度
    public int size() {
        return size;
    }

    // 显示链表所有元素
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    // 清空链表
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = null;
            cur = next;
        }
        head = null;
        size = 0;
    }
}
